<%@page import="BusinessLogic.Result.ResultType"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="BusinessLogic.Result" %>
<%@ page import="com.oreilly.servlet.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.net.URLEncoder" %>
<title>EasySchedule</title>
<link href="public/style.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="public/jquery.js"></script>
<script type="text/javascript" src="public/courseValidations.js"></script>
<script type="text/javascript">
	function goToAcnhor() { 
		window.location.hash="errorAnchor"; 
	}
</script>
</head>
	<jsp:useBean id="requestHandler" class="RequestHandlerPackage.RequestHandler" scope="application"/>
	<%
	
	String cname,group,capacity,points;
	cname = group = capacity = points ="";
	String error="";
	if (request.getMethod().equals("POST"))
	{
		Map<String, String[]> params = new HashMap<String,String[]>();
		params.put("action", new String[]{"createNewCourse"});
		String me = request.getUserPrincipal().getName(); 
		params.put("ADMIN", new String[]{me});
		
		Enumeration<String> map = request.getParameterNames();
		while(map.hasMoreElements())
		{
			String par = (String) map.nextElement();
			params.put(par, request.getParameterValues(par));
		}
		Result res = requestHandler.handle(params);
		if (res.getResultType().equals(ResultType.SUCCESS))
				{
					String redirect = request.getContextPath() + "/";
					response.sendRedirect(redirect);
				}
		else{
			error = "ERROR: " +res.getFailureMessage();
			cname = params.get("NAME")[0];
			group = params.get("GROUP_NUM")[0];
			capacity = params.get("CAPACITY")[0];
			points = params.get("POINTS")[0];
		}
	}
	String anchor = error.isEmpty()?"": "onload=\"goToAcnhor()\"";
	%>
<body <%=anchor %>>
    <div id="page">
	        <div id="header">
	            <h1><a href="./">Easy<span class="logo">Schedule</span></a></h1>
	            <p id="subtitle">Nice and easy registration system</p>
	        </div>
	        <div id="menu">
	            <ul>
	                <li class="left_menu"><a href="./">Home</a></li>
	                <li class="left_menu"><a href="./about.html">About</a></li>
	                <li class="left_menu"><a href="/tamirandevyatar/logout">Logout</a></li>
	            </ul>
	        </div>
        <div id="main">	
        	<div id="sidebar">
                <div class="box" id="login">
                    <h2 >Menu</h2>
                    <div class="box2">
                        <ul>
                            <li><a href="/tamirandevyatar/allCourses">Show courses</a></li>
                            <li><a href="/tamirandevyatar/addCourse">Add course</a></li>
                            <li><a href="/tamirandevyatar/users">Manage users</a></li>
                            <li><a href="/tamirandevyatar/update">Update my details</a></li>
                        </ul>
                    </div>
                </div><!-- box -->				
            </div><!-- sidebar -->    	              
            <div id="content">
                <div id="contheader">
                    <h2>Adding A Course</h2>
                </div>		
                <div id="content2" class="welcome">	
					<div class="entry">
						<form id="addCoursForm" action="" method="post" onsubmit="return validateAddCourseForm()">
							<label id="generalError" class="error"><%=error%></label>
							<p><a id="errorAnchor"></a>&nbsp;Please fill out the information below: (All the fields are required)</p>
					    	<table border="0">
					    		<tr>
								<td>
					    		<label for="NAME">The new course name &nbsp;&nbsp;</label>
					    		</td>
					    		<td>
						    	<input type="text" name="NAME" value="<%=cname %>" id="NAME"/>
						    	<label id="NAME_ERROR" class="error"></label>
						    	</td>
						    	</tr>
						    	<tr>
								<td>
						    	<label for="DESCRIPTION">Short description &nbsp;&nbsp;</label>
						    	</td>
						    	<td>
						    	<textarea id="DESCRIPTION" cols="20" rows="3" name="DESCRIPTION"></textarea>
		              	        <label id="DESC_ERROR" class="error"></label>
								</td>
								</tr>
								<tr>
								<td>
								<label for="POINTS">Number of credit points&nbsp;&nbsp;</label>
						    	</td>
						    	<td>
						    	<input type="text" name="POINTS" value="<%=points %>" id="POINTS"/>
								<label id="POINTS_ERROR" class="error"></label>
		                        </td>
		                        </tr>
		                        <tr>
								<td>
								<label for="GROUP_NUM">Group number&nbsp;&nbsp;</label>
						    	</td>
						    	<td>
						    	<input type="text" name="GROUP_NUM" value="<%=group %>" id="GROUP_NUM"/>
						    	<label id="GROUP_ERROR" class="error"></label>
								</td>
								</tr>
								<tr>
								<td>
								<label for="CAPACITY">Max capacity&nbsp;&nbsp;</label>
						    	</td>
						    	<td>
						    	<input type="text" name="CAPACITY" value="<%=capacity %>" id="CAPACITY"/>
						    	<label id="CAPACITY_ERROR" class="error"></label>
								</td>
								</tr>
							</table>
							
							<label>&nbsp;Every course must has at least one session:&nbsp;&nbsp;</label>
							<br/>
							<table>
								<tr>
								<td>
								<label for="DAY">Day&nbsp;&nbsp;</label>
						    	</td>
						    	<td>
						    	<select name="DAY"  id="DAY">
									<option value="Sunday">Sunday</option>
									<option value="Monday">Monday</option>
									<option value="Tuesday">Tuesday</option>
									<option value="Wednesday">Wednesday</option>
									<option value="Thursday">Thursday</option>
									<option value="Friday">Friday</option>
									<option value="Saturday">Saturday</option>
								</select>
						    	<label id="DAY_ERROR" class="error"></label>
								</td>
								</tr>
								<tr>
								<td>
								<label for="START_TIME">Start time&nbsp;&nbsp;</label>
						    	</td>
						    	<td>
						    	<select name="START_TIME" id="START_TIME">
									<option value="8">08:00</option>
									<option value="9">09:00</option>
									<option value="10">10:00</option>
									<option value="11">11:00</option>
									<option value="12">12:00</option>
									<option value="13">13:00</option>
									<option value="14">14:00</option>
									<option value="15">15:00</option>
									<option value="16">16:00</option>
									<option value="17">17:00</option>
								</select>
						    	<label id="START_ERROR" class="error"></label>
								</td>
								</tr>
								<tr>
								<td>
								<label for="END_TIME">End time&nbsp;&nbsp;</label>
						    	</td>
						    	<td>
						    	<select name="END_TIME" id="END_TIME">
									<option value="9">09:00</option>
									<option value="10">10:00</option>
									<option value="11">11:00</option>
									<option value="12">12:00</option>
									<option value="13">13:00</option>
									<option value="14">14:00</option>
									<option value="15">15:00</option>
									<option value="16">16:00</option>
									<option value="17">17:00</option>
									<option value="18">18:00</option>
								</select>
						    	<label id="END_ERROR" class="error"></label>
						    	</td>
						    	</tr>							
							</table>
							<br/>
						    <h2>
						    	<input type="submit" value="Add course" />
						    	<input type="reset" value="Reset" />
					    	</h2>
				  		</form>
					</div>
				
                </div><!-- content2 -->								              
            </div><!-- content -->                    
            <div class="clearing">&nbsp;</div> 
            <div id="footer">
                <p><a href="http://www.cs.technion.ac.il/~cs236369/">Managing Data on the World-Wide Web</a></p>
            </div>
        </div><!-- main -->
    </div><!-- page -->
	</body>
</html>